<template>
  <div style="padding: 10px">
    <div style="margin:10px">
      <el-button type="primary" @click="add" size="large">新增规则</el-button>
    </div>
<!--    <div style="margin:10px 0">-->
<!--      <el-input v-model="search" placeholder="请输入您要查找的年份" style="width:30%"/>-->
<!--      <el-button type="primary" style="margin-left:10px" @click="load">查询</el-button>-->
<!--    </div>-->


    <el-table :data="tableData"  class="custom-table" stripe border style="width: 100%" clearable>
      <el-table-column prop="id" label="id" sortable auto width="150"/>
      <el-table-column prop="year" label="年份" width="100"/>
      <el-table-column prop="grade" label="年级" width="100"/>
      <el-table-column prop="rule" label="规则" width="200" />
      <el-table-column prop="uptime" label="录入时间" width="200" />
      <el-table-column label=" 操作" width="150">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm title="确定删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px">
      <el-pagination
          :page-size="pageSize"
          :current-page="currentPage"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

      <el-dialog v-model="dialogVisible" title="新增规则" width="30%">
        <el-form :model="form" label-width="120px">
          <el-form-item label="年份">
            <el-input-number v-model="form.year" :min="2020" :max="4000" @change="handleChange" value-on-clear="min"/>
          </el-form-item>
          <el-form-item label="年级">
            <el-radio v-model="form.grade" label="研一" size="large" >研一</el-radio>
            <el-radio v-model="form.grade" label="研二" size="large" >研二</el-radio>
            <el-radio v-model="form.grade" label="研三" size="large" >研三</el-radio>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="choose">下一步</el-button>
      </span>
        </template>
      </el-dialog>

      <!--      研一 -->
      <div>
        <el-dialog v-model="dialogVisible1" title="新增规则" width="30%">
          <el-form :model="form1" label-width="120px">

            <div style="text-align: center; font-size: 22px">学位成绩</div>
            <el-form-item label="学位成绩权重">
              <el-input-number v-model="form1.学业成绩.学位成绩权重" :precision="2" :step="0.01" :min="0" :max="10" />
            </el-form-item>
          </el-form>

          <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogVisible1 = false">取消</el-button>
                <el-button type="primary" @click="save1">确定</el-button>
              </span>
          </template>
        </el-dialog>
      </div>

      <!--    研二-->
      <div>
        <el-dialog v-model="dialogVisible2" title="新增规则" width="30%">
          <el-form :model="form2" label-width="120px">

            <div style="text-align: center; font-size: 22px">学业成绩</div>
            <el-form-item label="学位成绩权重">
              <el-input-number v-model="form2.学业成绩.学位成绩权重" :precision="2" :step="0.01" :min="0" :max="10" />
            </el-form-item>

            <div style="text-align: center; font-size: 22px">思政表现</div>
            <div style="text-align: center; font-size: 14px">荣誉分</div>
            <el-form-item label="优秀">
              <el-input-number v-model="form2.思政表现.荣誉分.优秀" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="良好">
              <el-input-number v-model="form2.思政表现.荣誉分.良好" :min="0" :max="100"/>
            </el-form-item>
            <div style="text-align: center; font-size: 14px">通报</div>
            <el-form-item label="通报每次扣分">
              <el-input-number v-model="form2.思政表现.通报批评扣分数" :min="0" :max="100"/>
            </el-form-item>
            <!--            <el-form-item label="合格">-->
            <!--              <el-input-number v-model="form3.思政表现.荣誉分.合格" :min="0" :max="100"/>-->
            <!--            </el-form-item>-->
            <div style="text-align: center; font-size: 14px">导师组评价</div>
            <el-form-item label="优秀">
              <el-input-number v-model="form2.思政表现.教师评价分.优秀" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="良好">
              <el-input-number v-model="form2.思政表现.教师评价分.良好" :min="0" :max="100"/>
            </el-form-item>
            <div style="text-align: center; font-size: 14px">学术论文</div>
            <el-form-item label="一类学术论文分数">
              <el-input-number v-model="form2.学术论文.一类" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="二类学术论文分数">
              <el-input-number v-model="form2.学术论文.二类" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="三类学术论文分数">
              <el-input-number v-model="form2.学术论文.三类" :min="0" :max="100"/>
            </el-form-item>


          </el-form>

          <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogVisible2 = false">取消</el-button>
                <el-button type="primary" @click="save2">确定</el-button>
              </span>
          </template>
        </el-dialog>
      </div>
      <!--研三-->

      <div>
        <el-dialog v-model="dialogVisible3" title="新增规则" width="30%">
          <el-form :model="form3" label-width="120px">

            <div style="text-align: center; font-size: 22px">思政表现</div>
            <div style="text-align: center; font-size: 14px">荣誉分</div>
            <el-form-item label="优秀">
              <el-input-number v-model="form3.思政表现.荣誉分.优秀" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="良好">
              <el-input-number v-model="form3.思政表现.荣誉分.良好" :min="0" :max="100"/>
            </el-form-item>
<!--            <el-form-item label="合格">-->
<!--              <el-input-number v-model="form3.思政表现.荣誉分.合格" :min="0" :max="100"/>-->
<!--            </el-form-item>-->
            <div style="text-align: center; font-size: 14px">导师组评价</div>
            <el-form-item label="优秀">
              <el-input-number v-model="form3.思政表现.教师评价分.优秀" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="良好">
              <el-input-number v-model="form3.思政表现.教师评价分.良好" :min="0" :max="100"/>
            </el-form-item>
            <div style="text-align: center; font-size: 14px">通报</div>
            <el-form-item label="通报每次扣分">
              <el-input-number v-model="form3.思政表现.通报批评扣分数" :min="0" :max="100"/>
            </el-form-item>
            <div style="text-align: center; font-size: 14px">学术论文</div>
            <el-form-item label="一类学术论文分数">
              <el-input-number v-model="form3.学术论文.一类" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="二类学术论文分数">
              <el-input-number v-model="form3.学术论文.二类" :min="0" :max="100"/>
            </el-form-item>
            <el-form-item label="三类学术论文分数">
              <el-input-number v-model="form3.学术论文.三类" :min="0" :max="100"/>
            </el-form-item>


          </el-form>

          <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogVisible3 = false">取消</el-button>
                <el-button type="primary" @click="save3">确定</el-button>
              </span>
          </template>
        </el-dialog>
    </div>

  </div>


</template>

<script>
import request from "../../utils/request"; //!!!!!!!

export default {
  name: 'Srules',
  components: {},
  data() {
    return {
      form: {

      },
      form1: {
        "学业成绩":{}
      },
      form2: {
        "学业成绩": {},
        "思政表现": {
          "荣誉分":{},
          "教师评价分":{},

        },
        "学术论文":{}
      },
      form3: {
        "思政表现":{
          "荣誉分":{},
          "教师评价分":{},
        },
        "学术论文":{}
      },
      search: "",
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      dialogVisible : false,
      dialogVisible1 : false,
      dialogVisible2 : false,
      dialogVisible3 : false,

    }
  },
     created(){
    this.load()
    this.form.year = 2024
  },
  methods:{
    load(){
      request.get("/rules2",{
        params: {
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        console.log(res.data);
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    add(){
      this.dialogVisible=true;
      this.form= {}
      this.load()

    },
    choose() {
      this.dialogVisible = false;
      if (this.form.grade === "研一") {
        this.dialogVisible1 = true;
      } else if (this.form.grade === "研二") {
        this.dialogVisible2 = true;
      } else {
        this.dialogVisible3 = true;
      }
    },
    save1(){

      console.log(this.form1);
      const requestBody = {
        year: this.form.year,
        grade: this.form.grade,
        rule: JSON.stringify(this.form1),
      };
      request.post("/rules2", requestBody).then(res => {
        console.log(res);
        if (res.code === '0') {
          this.$message({type: "success", message: "增加成功"});
        } else {
          this.$message({type: "error", message: "增加失败"});
        }
      });
    },

    save2(){
      console.log(this.form2);
      const requestBody = {
        year: this.form.year,
        grade: this.form.grade,
        rule: JSON.stringify(this.form2)
      };
      request.post("/rules2", requestBody).then(res => {
        console.log(res);
        if (res.code === '0') {
          this.$message({type: "success", message: "增加成功"});
        } else {
          this.$message({type: "error", message: "增加失败"});
        }
      });
    },

    save3(){
      console.log(this.form3);
      const requestBody = {
        year: this.form.year,
        grade: this.form.grade,
        rule: JSON.stringify(this.form3)
      };
      request.post("/rules2", requestBody).then(res => {
        console.log(res);
        if (res.code === '0') {
          this.$message({type: "success", message: "增加成功"});
        } else {
          this.$message({type: "error", message: "增加失败"});
        }
      });
    },

    handleEdit(row){
      this.form =JSON.parse(JSON.stringify(row))
      this.dialogVisible =true
    },

    handleSizeChange(pageSize){
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum){
      this.currentPage = pageNum
      this.load()
    },
    handleDelete(id){
      console.log(id)
      request.delete('/rules2/'+ id).then(res =>{
        if(res.code === '0'){
          this.$message({
            type:"success",
            message:"删除成功"
          })
        } else{
          this.$message({
            type:"error",
            message:res.msg
          })
        }
        this.load()
      })
    },

  },
}
</script>

<style>

</style>
